package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    /**
     * 分页查询
     *
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10")int size){
        PageInfo info = moduleService.findAll(page,size);
        request.setAttribute("page",info);
        return "system/module/module-list";
    }

    /*
    进入保存页面
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        //查询所有模块，构造下拉框
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);

        return "system/module/module-add";
    }

    /**
     * 新增或者修改
     */
    @RequestMapping("/edit")
    public String edit(Module module){
        if (StringUtils.isEmpty(module.getId())){
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    //更新模块
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);
        return "system/module/module-update";
    }

    //删除模块
    @RequestMapping("/delete")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
